package com.chinacoal.microservice.impl.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.chinacoal.microservice.model.wofkflow.resource.WorkflowVar;
import com.chinacoal.microservice.model.wofkflow.resource.WrapAttribute;
import com.eos.workflow.data.WFActivityDefine;

/**
 * <p>Title: WorkFlowFun</p>
 * <p>Description: 流程相关方法</p>
 * @author sunh
 * @date 2019年8月7日 下午5:48:56
 */
public class ProcessFun {

	/**
	 * <p>Title: getOpcodeName</p>
	 * <p>Description: 获取操作码名称</p>
	 * @param opcode
	 * @return
	 */
	public static String getOpcodeName(String opcode) {

		if (WorkflowVar.APPROVE_AGREED.equals(opcode)) return WorkflowVar.APPROVE_AGREED_NAME;// 1000 同意
		else if (WorkflowVar.APPROVE_REJECTED.equals(opcode)) return WorkflowVar.APPROVE_REJECTED_NAME; // 1001 驳回
		else if (WorkflowVar.APPROVE_RECALL.equals(opcode)) return WorkflowVar.APPROVE_RECALL_NAME; // 1002 撤回
		else if (WorkflowVar.APPROVE_FALLBACK.equals(opcode)) return WorkflowVar.APPROVE_FALLBACK_NAME; // 1003 回退
		else if (WorkflowVar.APPROVE_CONSULT.equals(opcode)) return WorkflowVar.APPROVE_CONSULT_NAME; // 1004 征询
		else if (WorkflowVar.APPROVE_DELEGATE.equals(opcode)) return WorkflowVar.APPROVE_DELEGATE_NAME; // 1005 委派
		else if (WorkflowVar.APPROVE_TRANSPOND.equals(opcode)) return WorkflowVar.APPROVE_TRANSPOND_NAME; // 1006 转发
		else if (WorkflowVar.APPROVE_REPEAL.equals(opcode)) return WorkflowVar.APPROVE_REPEAL_NAME; // 1007 撤销审批
		else if (WorkflowVar.APPROVE_TERMINATE.equals(opcode)) return WorkflowVar.APPROVE_TERMINATE_NAME; // 1008 流程终止
		else if (WorkflowVar.APPROVE_HANGUP.equals(opcode)) return WorkflowVar.APPROVE_HANGUP_NAME; // 1009 流程挂起
		else if (WorkflowVar.APPROVE_PENDING.equals(opcode))	return WorkflowVar.APPROVE_PENDING_NAME; // 1011 待审批
		else return "其他";
	}

	/**
	 * <p>Title: getPage</p>
	 * <p> Description: 获取分页组件 </p>
	 * @param <T>
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public static <T> Page<T> getPage(Integer page, Integer pageSize) {

		if (page == null || pageSize == null || page < 1 || pageSize < 1)
			return new Page<T>(0, Integer.MAX_VALUE);

		return new Page<T>(page, pageSize);
	}

	/**
	 * <p>Title: parseAttrXML </p>
	 * <p> Description: 工作流扩展属性解析方法 </p>
	 * @param xml 工作流扩展属性xml串
	 * @return
	 */
	public static List<Map<String, String>> parseAttrXML(String xml) {
		List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
		try {
			Document doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			@SuppressWarnings("unchecked")
			Iterator<Element> iterator = root.elementIterator("extendNode");
			while (iterator.hasNext()) {
				Element element = iterator.next();
				Map<String, String> map = new HashMap<String, String>();
				map.put("key", element.elementTextTrim("key"));
				map.put("value", element.elementTextTrim("value"));
				map.put("desc", element.elementTextTrim("desc"));
				resultList.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, String>>();
		}

		return resultList;
	}


	/**
	 * <p>Title: getOptionListWAD </p>
	 * <p> Description: wfActivityDefines列表转换 WrapAttribute列表 </p>
	 * @param wfActivityDefines
	 * @param isExclude         是否排除非审批节点和填报节点
	 * @return
	 */
	public static List<WrapAttribute> getOptionListWAD(List<WFActivityDefine> wfActivityDefines, boolean isExclude, String editActivityId) {
		List<WrapAttribute> params = new ArrayList<WrapAttribute>();

		if (wfActivityDefines == null)
			return params;

		for (WFActivityDefine wfActivityDefine : wfActivityDefines) {
			WrapAttribute param = new WrapAttribute();
			String id = wfActivityDefine.getId();
			String name = wfActivityDefine.getName();
			String type = wfActivityDefine.getType();
			String activityParentId = String.valueOf(wfActivityDefine.getProcessDefId());
			
			if (isExclude) {
				if ("start".equals(type))
					continue;
				if ("finish".contains(type))
					continue;
				if (id.equals(editActivityId))
					continue;
			}

			param.setId(id);
			param.setName(name);
			param.setType(type);
			param.setActivityParentId(activityParentId);
			params.add(param);
		}

		return params;
	}

	/**  
	 * @Package: com.chinacoal.microservice.impl.util.common.ProcessFun.getExceptionMessage
	 * @MethodName: getExceptionMessage
	 * @MethodDesc: 获取异常消息
	 * @ParamList: {
	 *				 @param e
	 *				 @return String
	 *			   }
	 * @Createby: sunh
	 * @CreatedDate: 2019-10-29 14:14:54
	 */ 
	public static String getExceptionMessage(Exception e) {
		String msg = "";
		if(e == null) {

		}else if(e.getCause() == null) {
			msg = e.getMessage();
		}else {
			Throwable throwable = e.getCause();
			
			while(throwable != null){
				if(StringUtils.isEmpty(throwable.getMessage())) {
					throwable = throwable.getCause();
				}else {
					msg = throwable.getMessage();
					throwable = throwable.getCause();
				}
			}
		}
		return msg;
	}
}
